package cn.dqy.gotanserver;

import cn.dqy.gotanserver.support.context.SpringContextSupport;
import cn.dqy.gotanserver.support.msghandle.MsgProcessHanldeRegistrar;
import cn.dqy.gotanserver.support.msghandle.MsgProcessInterface;
import cn.dqy.gotanserver.support.msghandle.impl.SimpleMsgProcess;
import cn.dqy.gotanserver.support.msghandle.impl.TickMsgProcess;
import cn.dqy.gotanserver.support.msghandle.impl.VideoStreamMsgProcess;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.scheduling.annotation.Async;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.io.File;
import java.io.IOException;
import java.util.function.Function;

@EnableSwagger2
@SpringBootApplication
public class GoTanServerApplication implements ApplicationListener<ContextRefreshedEvent> {
    static Logger logger = LoggerFactory.getLogger(GoTanServerApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(GoTanServerApplication.class, args);
    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        logger.info("ContextRefreshed!!!开始注册消息处理器");
        MsgProcessHanldeRegistrar registrar = SpringContextSupport.getContext().getBean(MsgProcessHanldeRegistrar.class);
        registrar.registrar((p)->{ return  "<TICK>".equals(p);}, TickMsgProcess.class);
        registrar.registrar((p)->{return p!=null && p.indexOf("<cammer:ps>::")!=-1;}, VideoStreamMsgProcess.class);
        registrar.registrar((p)->{return false;},SimpleMsgProcess.class);
        logger.info("ProcessMap = {}",registrar.getMsgProcessInterfaceMap());
    }
}
